package woaini.wind.ai.controller;

import lombok.Data;

@Data
public class AIResponse {

  public AIResponse(String actualContent) {

    // 正则获取标签 <think></think>之间的内容
    this.think = extractContentBetweenTags(actualContent, "<think>", "</think>");
    this.content = extractContentAfterTag(actualContent, "</think>");
    // 替换/r/n为换行
    this.content = this.content.replaceAll("\\r", "\n").replaceAll("\\n", "\n");
    this.think = this.think.replaceAll("\\r", "\n").replaceAll("\\n", "\n");
  }
  private String think;

  private String content;

  private static String extractContentBetweenTags(String input, String startTag, String endTag) {
    int startIndex = input.indexOf(startTag) + startTag.length();
    int endIndex = input.indexOf(endTag);
    if (startIndex < startTag.length() || endIndex == -1) {
      return ""; // Return empty string if tags are not found or in wrong order
    }
    return input.substring(startIndex, endIndex).trim();
  }

  private static String extractContentAfterTag(String input, String tag) {
    int tagIndex = input.indexOf(tag) + tag.length();
    if (tagIndex < tag.length()) {
      return ""; // Return empty string if tag is not found
    }
    return input.substring(tagIndex).trim();
  }
}
